package org.example;

import org.example.binding.MapperRegistry;
import org.example.mapping.MappedStatement;
import org.example.mapping.ParameterMap;
import org.example.session.SqlSession;

import java.util.HashMap;
import java.util.Map;

public class Configuration {


    private Environment environment;


    private TypeAliasRegistry registry = new TypeAliasRegistry();


    private Map<String, ParameterMap> parameterMapRegister = new HashMap<>();


    private Map<String, MappedStatement> mappedStatements = new HashMap<>();


    private MapperRegistry mapperRegistry = new MapperRegistry(this);





    public Configuration() {
        // 注册事务工厂
        registry.registerType("JDBC",JdbcTransactionFactory.class);
        registry.registerType("DRUID",DruidDataSourceFactory.class);
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }


    public Class<?> getType(String alias) {
        return registry.getType(alias);
    }

    public void registerType(String alias,Class<?> type) {
        registry.registerType(alias,type);
    }


    public void addParameterMap(ParameterMap parameterMap) {
        parameterMapRegister.put(parameterMap.getId(),parameterMap);
    }


    public ParameterMap getParameterMap(String id) {
        return parameterMapRegister.get(id);
    }


    public void addMappedStatement(MappedStatement mappedStatement) {
        String statementId = mappedStatement.getId();
        mappedStatements.put(statementId,mappedStatement);
    }

    public <T> void addMapper(Class<T> type) {
        this.mapperRegistry.addMapper(type);
    }


    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
       return this.mapperRegistry.getMapper(type,sqlSession);
    }


    public MappedStatement getMappedStatement(String statement) {
        return mappedStatements.get(statement);
    }










}
